//
// Created by myrica on 4/25/25.
//
#include "Pass/MoveAllocaLLVMIR.h"

// PreservedAnalyses MoveAllocaToEntryPass::run(Function &F, FunctionAnalysisManager &) {
//     BasicBlock &Entry = F.getEntryBlock();
//     Instruction *InsertPt = &*Entry.getFirstInsertionPt();
//     std::vector<AllocaInst *> AllocasToMove;
//
//     for (BasicBlock &BB: F) {
//         for (Instruction &I: BB) {
//             if (auto *AI = dyn_cast<AllocaInst>(&I)) {
//                 if (AI->getParent() != &Entry) {
//                     AllocasToMove.push_back(AI);
//                 }
//             }
//         }
//     }
//
//     for (AllocaInst *AI: AllocasToMove) {
//         AI->moveBefore(InsertPt);
//     }
// }

llvm::PassPluginLibraryInfo getMoveAllocaToEntryPassPluginInfo() {
    return {
        LLVM_PLUGIN_API_VERSION, "MoveAllocaToEntryPass", LLVM_VERSION_STRING,
        [](PassBuilder &PB) {
            PB.registerPipelineParsingCallback(
                [](StringRef Name, FunctionPassManager &FPM,
                   ArrayRef<PassBuilder::PipelineElement>) {
                    if (Name == "move-alloca-to-entry") {
                        FPM.addPass(MoveAllocaToEntryPass());
                        return true;
                    }
                    return false;
                });
        }
    };
}
